from os import makedirs

import requests
import re
from bs4 import BeautifulSoup
from contextlib import closing

from os.path import exists
import sys

sys.setrecursionlimit(1000000)


def req_get_bf(url):
    get = requests.get(url, timeout=180)
    get.encoding = 'utf-8'
    return BeautifulSoup(get.text, 'html.parser')


totalCount = 0


def just_url(page):
    global totalCount
    url = "http://m.17989.com/xiaohua/" + str(page) + ".htm"
    temp_bf = req_get_bf(url)
    pre = temp_bf.find_all('pre')
    if pre:
        for agent in pre:
            agent_string = agent.string
            if not bool(re.search('[a-z]', agent_string)):
                string = "<item>" + agent_string + "</item>"
                totalCount = totalCount + 1
                print(page, totalCount, string)
                with open('joke.txt', 'a') as file_object:  # windows 系统中 ，路径间隔符号是反斜杠 \
                    file_object.write(string)
                    file_object.write("\n")
    just_url(page + 1)


if __name__ == '__main__':
    just_url(1)
